/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package webservice;

import database.Item;
import database.ItemFacadeLocal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

/**
 * REST Web Service
 *
 * @author Ewoud
 */
@Path("restful/item")
public class RESTfulResource {
    ItemFacadeLocal itemFacade = lookupItemFacadeLocal();

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of RESTfulResource
     */
    public RESTfulResource() {
    }

    /**
     * Retrieves representation of an instance of webservice.RESTfulResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("text/plain")
    public String getText() {
        //TODO return proper representation object
        return "Some text that is send using RESTful";
    }

    /**
     * PUT method for updating or creating an instance of RESTfulResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("text/plain")
    public void putText(String content) {
    }
    
    /**
     *  HTTP DELETE requests.
     */
    @DELETE
    @Path("{id}")
    public void deleteItem(@PathParam("id") String itemId) {
        Item deletingItem = itemFacade.find(Long.parseLong(itemId));
        if(deletingItem != null){
            itemFacade.remove(deletingItem);
        }
    }

    private ItemFacadeLocal lookupItemFacadeLocal() {
        try {
            javax.naming.Context c = new InitialContext();
            return (ItemFacadeLocal) c.lookup("java:global/BakkerijX_v2/BakkerijX_v2-ejb/ItemFacade!database.ItemFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
